package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 根据身份证号查询客户
 * @author 许海宜
 * 2020年4月7日
 */
public class QuerySameClient implements ServerInterface {

	@SuppressWarnings("rawtypes")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//入参转换
			CtmInfoBean bean = JsonUtils.objectToPojo(req.getData(), CtmInfoBean.class);
			if(StringUtils.isBlank(bean.getCertNo())){
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "证件号码不能为空。", Void.class);
			}
			//获取ctmMapper
			CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
			//根据证件号码模糊查询客户列表
			List<CtmInfoBean> ctmList = ctmInfoMapper.querySameClient(bean.getCertNo(), CtmInfoBean.CTM);
			//判断是否有查询结果
			if(ctmList == null) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "证件号码不存在。", Void.class);
			}
			Map<String, String> cdl = Tools.mapDicByCode(factory, "CDL");//信用等级
			Map<String, String> ctt = Tools.mapDicByCode(factory, "CTT");//客户类型
			Map<String, String> idt = Tools.mapDicByCode(factory, "IDT");//证件类型
			for (CtmInfoBean ctm : ctmList) {
				ctm.setCreditLevelName(cdl.get(ctm.getCreditLevel()));
				ctm.setCtmTypeName(ctt.get(ctm.getCtmType()));
				ctm.setCertTypeName(idt.get(ctm.getCertType()));
			}
			/*//结果集
			List<QuerySameClientVO> vos = new ArrayList<QuerySameClientVO>();
			//获取accountMapper
			AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
			for (CtmInfoBean ctm : ctmList) {
				QuerySameClientVO vo = new QuerySameClientVO();
				vo.setCtm(ctm);
				//根据客户编号查询账户列表
				vo.setAccounts(accountInfoMapper.getAccountInfoByCtmNo(ctm.getCtmNo(), AccountInfoBean.ACCOUNT));
				vos.add(vo);
			}*/
			//返回结果集
			MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "sucess", List.class);
			msg.setData(ctmList);
			return msg;
		}catch (Exception e) {
			logger.error("根据身份证号查询客户查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", Void.class);
		}
	}

}
